<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="审批意见" prop="approvalComments">
            <el-input v-model="formItem.approvalComments" placeholder="请输入审批意见" type="textarea" :autosize="{ minRows: 4, maxRows: 4 }" resize="none" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <div style="float:right">
            <el-button size="mini" @click="save(false)">驳回</el-button>
            <el-button size="mini" @click="save(true)">通过</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { countersignatureCreate as create, countersignatureDetail as detail } from '@neu/dcom-ui/api/alter'

const formItem = {
  // 工作流TaskId
  taskId: '',
  // 工单号
  workNum: '',
  // 审批意见
  approvalComments: '',
  // 操作人编码
  operatorCode: '',
  // 操作人姓名
  operatorName: '',
  // 是否同意
  whetherAgree: false
}

const formRules = {
  approvalComments: [{ required: true, message: '审批意见不能为空', trigger: 'blur' }]
}

export default {
  name: 'CountersignatureForm',
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: { ...formRules }
    }
  },
  created() {
    this.fetch()
  },
  methods: {
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        detail(this.workOrderNum, this.taskId).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    save(whetherAgree) {
      this.$refs.myform.validate((valid) => {
        if (valid) {
          this.formItem.whetherAgree = whetherAgree
          // 设置工单号和任务
          this.formItem.taskId = this.taskId
          this.formItem.workNum = this.workOrderNum
          // 调用后台
          create(this.formItem).then(res => {
            if (res.status === 'success') {
              this.$emit('cancelNewOrderForm', 'refresh')
            }
          })
        }
      })
    }
  }
}
</script>
